package com.ruimei.controller;

import com.ruimei.util.AliyunOSSClientUtil;
import com.ruimei.util.ImagesUtils;
import com.ruimei.util.MultToFile;
import com.ruimei.util.ResultMap;
import io.swagger.annotations.Api;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@CrossOrigin
@RestController
@Api(tags={"文件上傳"})
public class FtpFileUploadController
{
  @RequestMapping(value={"/ftpuploadimg"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  public ResultMap uploadImg(@RequestParam("files") MultipartFile[] files, HttpServletRequest request)
    throws IOException
  {
    List<String> url = new ArrayList();
    if ((files != null) && (files.length > 0)) {
      for (int i = 0; i < files.length; i++)
      {
        MultipartFile file = files[i];
        if (StringUtils.isNotBlank(file.getOriginalFilename())) {
          File f ;
          if((file.getSize()/1024/1024)>2){
            f = new ImagesUtils().compressPictures(MultToFile.analyzeFile(file, request),file);

          }
          else{
            f = new ImagesUtils().compressPictures(MultToFile.analyzeFile(file, request),file,1);
          }
          if(f.exists()){
            url.add(AliyunOSSClientUtil.uploadObject2OSS(AliyunOSSClientUtil.getOSSClient(),f , "sagiri", "teacher/"));
          }


        }
      }
    }
    return new ResultMap(url);
  }
}

